package com.songlanyun.modules.payrefund.controller.api;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.alibaba.fastjson.JSONObject;
import com.songlanyun.common.enums.OrderConstant;
import com.songlanyun.common.enums.RightsOrderConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.custom.MathUtils;
import com.songlanyun.common.wxUtils.PayCommon;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.exception.OrderException;
import com.songlanyun.modules.rabbitmq.mq.producer.SendOrderOrRightsOrderMsgService;
import com.songlanyun.pay.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.modules.payrefund.entity.PayRefundOrder;
import com.songlanyun.modules.payrefund.service.PayRefundOrderService;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 退款系统订单
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2020-11-24 18:09:07
 */
@RestController
@RequestMapping("api/pay/refund")
@Slf4j
public class PayRefundOrderApiController extends AbstractApiController {
    @Autowired
    private PayRefundOrderService payRefundOrderService;

    @Resource
    @Qualifier("payService2")
    private PayService payService;

    @Resource
    private SendOrderOrRightsOrderMsgService sendOrderOrRightsOrderMsgService;

    /**
     * 支付组件（com.songlanyun.pay）微信回调方式入口
     *
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/wxRefundNotify")
    public String wxRefundNotify(HttpServletRequest request, HttpServletResponse response) {
        String result = payService.callBackWxRefund(request, param -> {
            Map map = JSONObject.parseObject(JSONObject.toJSONString(param), Map.class);
            log.info("微信退款回调：{}", map);
            String outRefundNo = map.get("out_refund_no").toString();//本地订单
            String refundId = map.get("refund_id").toString();//第三方单号
            String refundFee = map.get("refund_fee").toString();//订单总金额，单位为分
            BigDecimal money = MathUtils.divide(new BigDecimal(refundFee), new BigDecimal("100"));

            try {
                PayRefundOrder payRefundOrder = payRefundOrderService.callbackHandle(outRefundNo, refundId, money, OrderConstant.PayMode.WE_CHAT);
                if (payRefundOrder != null && RightsOrderConstant.RightsRefundStatus.SUCCESS.equals(payRefundOrder.getRightsRefundStatus())) {
                    sendOrderOrRightsOrderMsgService.rightsRefundSuccessSendMsg(payRefundOrder);
                }
                return "success";
            } catch (RRException ex) {
                if (ex.getCode() == OrderException.REPEAT_REFUND.getCode()) {
                    return "success";
                }
            } catch (Exception ex) {
                return "fail";
            }

            return "fail";   //会继续发消息
        });
        return result;
    }
}
